﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cave : MonoBehaviour {
    public int SpawnCount;
    public float SpawnInterval;
    public GameObject MonsterType;

    private int _spawnCount;

    private void OnEnable()
    {
        Data.Instance.MonsterCount++;
        if (!MonsterType)
        {
            return;
        }
        _spawnCount = SpawnCount;
        StartCoroutine(Spawn());
    }

    private void OnDisable()
    {
        Data.Instance.MonsterCount--;
    }
    private IEnumerator Spawn()
    {
        while (_spawnCount > 0)
        {
            _spawnCount--;
            yield return new WaitForSeconds(SpawnInterval);
            ObjectPool.Instance.OutPool(MonsterType).transform.position=transform.position;
        }
        DestroyThis();
    }
    private void DestroyThis()
    {
        ObjectPool.Instance.InPool(gameObject);
    }
}
